import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { GuildEventName } from "../view/guild/GuildEventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { EGuildStructureType } from "../../../Message/Public/EGuildStructureType";
import { GameController } from "../mvc/GameController";
import { IView } from "../../mvc/interfaces/IView";
import { GuildManorListWindow } from "../view/guild/manor/GuildManorListWindow";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { GuildTabType } from "../view/guild/myGuild/GuildTabType";
import { EGuildPosition } from "../../../Message/Game/EGuildPosition";
import { GuildBuyManorWindow } from "../view/guild/manor/GuildBuyManorWindow";
import { AccelerateWindow } from "../view/guild/manor/AccelerateWindow";
import { StructureTimer } from "../view/guild/manor/StructureTimer";
import { GuildAwardConfig } from "../resource/GuildAwardConfig";
import { GameDefConfig } from "../resource/GameDefConfig";
import { GuildAuraWindow } from "../view/guild/manor/GuildAuraWindow";
import { SGuildStructureInfo } from "../../../Message/Game/SGuildStructureInfo";
import { IGuildModule } from "../../../modules/interfaces/IGuildModule";
type int = number;
//class GuildManorController
    
    export  class GuildManorController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.GuildOpenManorListWindow, this.onGuildOpenManorListWindowHandler);
            Dispatcher.addEventListener(GuildEventName.AccWindowOpen, this.onAccWindowOpenHandler);
            Dispatcher.addEventListener(EventName.GuildGetSpaceList, this.onGuildGetSpaceListHandler);
            Dispatcher.addEventListener(EventName.GuildGetSpaceListSuccess, this.onGuildGetSpaceListSuccessHandler);
            Dispatcher.addEventListener(EventName.OpenGuildBuyManorWindow, this.onOpenGuildBuyManorWindowHandler);
            Dispatcher.addEventListener(EventName.GuildGetStructure, this.onGuildGetStructureHandler);
            Dispatcher.addEventListener(EventName.GuildGetStructureSuccess, this.onGuildGetStructureSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildUpdateStruture, this.onGuildUpdateStrutureHandler);
            Dispatcher.addEventListener(EventName.GuildUpdateStrutureSuccess, this.onGuildUpdateStrutureSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildBuyManor, this.onGuildBuyManorHandler);
            Dispatcher.addEventListener(EventName.GuildBuyManorSuccess, this.onGuildBuyManorSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildGetAward, this.onGuildGetAwardHandler);
            Dispatcher.addEventListener(EventName.GuildGetAwardSuccess, this.onGuildGetAwardSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildOpenAuraWindow, this.onGuildOpenAuraWindowHandler);
            Dispatcher.addEventListener(EventName.GuildGetAuraEvent, this.onGuildGetAuraEventHandler);
            Dispatcher.addEventListener(EventName.GuildGetAuraEventSuccess, this.onGuildGetAuraEventSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildOpenManorListWindow, this.onOpenGuildManorListWindowHandler);
            Dispatcher.addEventListener(EventName.GuildEnterManor, this.onGuildEnterManorHandler);
            Dispatcher.addEventListener(EventName.GuildQuiteManor, this.onGuildQuiteManorHandler);
            Dispatcher.addEventListener(EventName.GuildConstructAccelerate, this.onGuildConstructAccelerateHandler);
            Dispatcher.addEventListener(EventName.GuildConstructAccelerateSuccess, this.onGuildConstructAccelerateSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildTreeAddAura, this.onGuildTreeAddAuraHandler);
            Dispatcher.addEventListener(EventName.GuildTreeAddAuraSuccess, this.onGuildTreeAddAuraSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildTreeAuraUpdate, this.onGuildTreeAuraUpdateHandler);
            Dispatcher.addEventListener(EventName.GuildTreeGetLeftTime, this.onGuildTreeGetLeftTimeHandler);
            Dispatcher.addEventListener(EventName.GuildTreeLeftTimeUpdate, this.onGuildTreeLeftTimeUpdateHandler);
            Dispatcher.addEventListener(EventName.GuildTreeShake, this.onGuildTreeShakeHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildTreeShakeCommand, this.onGuildTreeShakeCommandHandler);
            return;
        }

        /* internal  */onGuildTreeGetLeftTimeHandler(arg1: DataEvent): void
        {
            GameProxy.guildProxy.getGuildTreeRipeTime();
            return;
        }

        /* internal  */onGuildTreeLeftTimeUpdateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (this._auraWindow && this._auraWindow.isHide == false) 
            {
                this._auraWindow.updateLeftTime(loc1);
            }
            return;
        }

        /* internal  */updateGuildTreeLevel(): void
        {
            if (this._auraWindow && this._auraWindow.isHide == false) 
            {
                this._auraWindow.updateLevel(cache.guild.getStructureLevel(EGuildStructureType.convert(EGuildStructureType._EGuildTree)));
            }
            return;
        }

        /* internal  */onGuildTreeShakeHandler(arg1: DataEvent): void
        {
            GameProxy.guildProxy.shakeGuildTree();
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.GuildStructureUpdate, this.onGuildStructureUpdateHandler);
            return;
        }

        public init(): void
        {
            this._guildModule = GameController.guild.guildModule;
            return;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            return null;
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetAuraEvent));
            return;
        }

        /* internal  */onGuildTreeShakeCommandHandler(arg1: Object): void
        {
            if (this._auraWindow) 
            {
                this._auraWindow.shakeSuccess();
            }
            return;
        }

        /* internal  */onOpenGuildManorListWindowHandler(arg1: DataEvent): void
        {
            if (this._manorListWindow == null) 
            {
                this._manorListWindow = new GuildManorListWindow();
            }
            this._manorListWindow.show();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetSpaceList, {"camp": 0, "guildName":""}));
            return;
        }

        /* internal  */onGuildGetSpaceListHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["camp"] as int;
            var loc2=arg1.data["guildName"] as string;
            GameProxy.guildProxy.getGuildSpaceList(loc1, loc2);
            return;
        }

        /* internal  */onGuildGetSpaceListSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            if (loc1.length == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(57342));
            }
            if (this._manorListWindow && this._manorListWindow.isHide == false) 
            {
                this._manorListWindow.updateGuildDataProvider(loc1);
            }
            return;
        }

        /* internal  */onGuildGetStructureHandler(arg1: DataEvent): void
        {
            GameProxy.guildProxy.getGuildStructure();
            return;
        }

        /* internal  */onGuildGetStructureSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            cache.guild.structureArray = loc1;
            var loc2=cache.guild.getBuildingStructureInfo();
            if (loc2) 
            {
                this.initStructureTimer(loc2.type, loc2.leftTime);
            }
            if (this._guildModule && GuildTabType.curTabType == GuildTabType.GuildBuilding) 
            {
                this._guildModule.updateBuilding(loc1);
            }
            this.updateGuildTreeLevel();
            return;
        }

        /* internal  */onOpenGuildBuyManorWindowHandler(arg1: DataEvent): void
        {
            var loc1=cache.guild.myGuildInfo;
            if (loc1 == null || !(loc1.position == EGuildPosition._EGuildLeader)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(57371));
                return;
            }
            if (loc1.level < 2) 
            {
                MsgManager.showRollTipsMsg(Language.getString(57372));
                return;
            }
            if (this._buyManorWindow == null) 
            {
                this._buyManorWindow = new GuildBuyManorWindow();
            }
            this._buyManorWindow.show();
            return;
        }

        /* internal  */onGuildOpenManorListWindowHandler(arg1: DataEvent): void
        {
            if (null == this._manorListWindow) 
            {
                this._manorListWindow = new GuildManorListWindow();
            }
            this._manorListWindow.show();
            return;
        }

        /* internal  */onAccWindowOpenHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as EGuildStructureType;
            if (null == this._accWindow) 
            {
                this._accWindow = new AccelerateWindow();
            }
            this._accWindow.type = loc1;
            this._accWindow.show();
            return;
        }

        /* internal  */onGuildUpdateStrutureHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as EGuildStructureType;
            GameProxy.guildProxy.updateGuildStruture(loc1);
            return;
        }

        /* internal  */onGuildUpdateStrutureSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["type"] as EGuildStructureType;
            var loc2=arg1.data["remainTime"] as number;
            this.initStructureTimer(loc1, loc2);
            if (this._guildModule) 
            {
                this._guildModule.updateManorInfoPanel(loc1, loc2);
            }
            MsgManager.showRollTipsMsg(Language.getString(57373));
            this.updateGuildTreeLevel();
            return;
        }

        /* internal  */initStructureTimer(arg1: EGuildStructureType, arg2: number): void
        {
            var loc1=cache.guild.structureTimer;
            if (loc1 != null) 
            {
                loc1.leftTime = arg2;
            }
            else 
            {
                loc1 = new StructureTimer(arg2);
                loc1.type = arg1;
            }
            return;
        }

        /* internal  */onGuildBuyManorHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            GameProxy.guildProxy.buyGuildSpace(loc1);
            return;
        }

        /* internal  */onGuildBuyManorSuccessHandler(arg1: DataEvent): void
        {
            this._buyManorWindow.hide();
            MsgManager.showRollTipsMsg(Language.getString(57374));
            return;
        }

        /* internal  */onGuildGetAwardHandler(arg1: DataEvent): void
        {
            GameProxy.guildProxy.getAward();
            return;
        }

        /* internal  */onGuildGetAwardSuccessHandler(arg1: DataEvent): void
        {
            var loc3=null;
            var loc1=GuildAwardConfig.instance.getConfigByLevel(cache.guild.myGuildInfo.hallLevel);
            var loc2="";
            if (loc1) 
            {
                loc3 = GameDefConfig.instance.getEPrictUnitName(loc1.type);
                loc2 = Language.getStringByParam(57375, loc1.value, loc3, loc1.costContribute);
            }
            MsgManager.showRollTipsMsg(loc2);
            MsgManager.addTipText(loc2);
            return;
        }

        /* internal  */onGuildOpenAuraWindowHandler(arg1: DataEvent): void
        {
            if (this._auraWindow == null) 
            {
                this._auraWindow = new GuildAuraWindow();
            }
            this._auraWindow.show();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetStructure));
            return;
        }

        /* internal  */onGuildGetAuraEventHandler(arg1: DataEvent): void
        {
            GameProxy.guildProxy.getGuildAuraEvent();
            return;
        }

        /* internal  */onGuildGetAuraEventSuccessHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */onGuildEnterManorHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["guildId"] as int;
            var loc2=arg1.data["useContribution"] as Boolean;
            GameProxy.copy.enterGuild_async(loc1, loc2);
            if (this._manorListWindow && this._manorListWindow.isHide == false) 
            {
                this._manorListWindow.hide();
            }
            return;
        }

        /* internal  */onGuildQuiteManorHandler(arg1: DataEvent): void
        {
            GameProxy.copy.leftGuild_async();
            return;
        }

        /* internal  */onGuildConstructAccelerateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["type"] as EGuildStructureType;
            var loc2=arg1.data["mode"] as int;
            var loc3=arg1.data["time"] as int;
            GameProxy.guildProxy.accelerateUpdateGuildStruture(loc1, loc2, loc3);
            return;
        }

        /* internal  */onGuildConstructAccelerateSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["type"] as EGuildStructureType;
            var loc2=arg1.data["remainTime"] as number;
            this.initStructureTimer(loc1, loc2);
            return;
        }

        /* internal  */onGuildStructureUpdateHandler(arg1: SGuildStructureInfo): void
        {
            if (this._guildModule) 
            {
                this._guildModule.updateManorInfoPanel(arg1.type, 0, true);
            }
            return;
        }

        /* internal  */onGuildTreeAddAuraHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.guildProxy.addGuildReiki(loc1);
            return;
        }

        /* internal  */onGuildTreeAddAuraSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(57460));
            return;
        }

        /* internal  */onGuildTreeAuraUpdateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (this._auraWindow && this._auraWindow.isHide == false) 
            {
                this._auraWindow.updateAura(loc1);
            }
            return;
        }

        private /* var */_guildModule: IGuildModule;

        private /* var */_manorListWindow: GuildManorListWindow;

        private /* var */_buyManorWindow: GuildBuyManorWindow;

        private /* var */_auraWindow: GuildAuraWindow;

        private /* var */_accWindow: AccelerateWindow;
    }
